package com.ishansong.ticket.service.impl;

import com.ishansong.ticket.exception.ServiceDaoException;
import com.ishansong.ticket.exception.ServiceException;
import com.ishansong.ticket.model.Demo;
import com.ishansong.ticket.service.DemoService;
import com.matrixjoy.dal.dao.Dao;
import com.matrixjoy.dal.dao.exception.DaoException;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * Created by fengmeng on 15/9/9.
 */

@Component(value = "demoService")
public class DemoServiceImpl implements DemoService {

	@Autowired
	Dao dao;

	public Demo getDemo(Long id) throws ServiceDaoException, ServiceException {
		try {
			return (Demo) dao.get(Demo.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	public Long saveDemo(Demo demo) throws ServiceDaoException, ServiceException {
		try {
			return (Long) dao.save(demo);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	public void updateDemo(Demo demo) throws ServiceDaoException, ServiceException {
		try {
			dao.update(demo);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}

	}

	public Boolean deleteDemo(Long id) throws ServiceDaoException, ServiceException {
		try {
			return (Boolean) dao.delete(Demo.class, id);
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}

	/**
     * 
     */
	public List<Demo> getDemosByAge(Integer age) throws ServiceDaoException, ServiceException {
		try {
			@SuppressWarnings("unchecked")
			List<Serializable> ids = dao.getIdList("getDemosByAge", new Object[] { age });
			if (!CollectionUtils.isEmpty(ids)) {
				List<Demo> logins = dao.getList(Demo.class, ids);
				return logins;
			} else {
				return Collections.emptyList();
			}
		} catch (DaoException e) {
			throw new ServiceDaoException(e);
		}
	}
}
